package edu.whu.service;

import edu.whu.bean.Product;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductService {

    private Map<Integer, Product> products = Collections.synchronizedMap(new HashMap<>());

    public void save(Product product){
        products.put(product.getId(), product);
    }

    public void deleteById(int id){
        products.remove(id);
    }

    public Product selectById(int id){
        return products.get(id);
    }

    public List<Product> selectByName(String name){
        List<Product> result = new ArrayList<>();

        for (Product pro: products.values()){
            if(pro.getName().equals(name)){
                result.add(pro);
            }
        }
        return result;
    }

    public List<Product> selectAll(){
        return products.values().stream().toList();
    }

    public int clear(){
        int count = products.size();
        products.clear();
        return count;
    }




}
